<?php
    class Schedule_Model extends CI_Model
    {
        public function __construct()
        {
            $this->load->database();
        }

        //根据学号检查是否存在这条记录
        public function check_student_exist($id)
        {
            $querystr = "SELECT * FROM student WHERE id='".$id."';";
            $out = $this->db->query($querystr)->result();
            return count($out) > 0;
        }

        public function check_classtable_exist($terms)
        {
            $querystr = "SELECT * FROM class WHERE term='".$terms."';";
            $out = $this->db->query($querystr)->result();
            return count($out) > 0;
        }

        //根据学号取得记录
        public function get_student_by_id($id)
        {
            $querystr = "SELECT * FROM student WHERE id='".$id."';";
            return $this->db->query($querystr)->result();
        }

        //插入一个学生记录
        //返回是否成功
        public function insert_student($student)
        {
            $querystr = "INSERT INTO student(id, password) VALUES('".$student['id']."', '".$student['password']."');";
            return $this->db->query($querystr);
        }

        //插入一个课程记录
        //返回是否成功
        public function insert_class($class)
        {
            $querystr = "INSERT INTO class(id,classname,week,number,classroom,teacher,term) VALUES('%s','%s','%s','%s','%s','%s','%s')";
            $querystr = sprintf($querystr, $class['id'], $class['classname'], $class['week'], $class['number'], $class['classroom'], $class['teacher'], $class['term']);
            return $this->db->query($querystr);
        }

        //获取指定学号学生的课表
        public function get_class_table_by_id($id)
        {
            $querystr = "SELECT * FROM class WHERE id='".$id."';";
            return $this->db->query($querystr)->result();
        }

        //获取指定学号和学期的学生课表
        public function get_class_table_by_id_and_term($id, $terms)
        {
            $querystr = "SELECT * FROM class WHERE id='".$id."' AND term='".$terms."';";
            return $this->db->query($querystr)->result();
        }
    }
?>